import time
import os
from common.save_records import SaveFile, MySQL
from pages.johnlewis import Johnlewis


def spider_johnlewis(excel_file_name, product_url):
    print("Task:%s..is staring" % excel_file_name)
    create_time = time.strftime('%Y-%m-%d-%H-%M', time.localtime(time.time()))

    log_dir = "D:\\pycode\\JT_spider\\%s" % excel_file_name
    if not os.path.exists(log_dir):
        os.mkdir(log_dir)

    excel_file_full_path_name = os.path.join(log_dir, '%s_%s.xlsx' % (excel_file_name, create_time))
    produce_img_dir_path = "%s\\img" % log_dir

    save_files = SaveFile(excel_file_full_path_name, produce_img_dir_path)
    excel_file, excel_file_format, file_object = save_files.create_excel()
    if not os.path.exists(produce_img_dir_path):
        os.mkdir(produce_img_dir_path)

    spider = Johnlewis(product_url)

    row_num = 1
    for info in spider.get_all_items():
        img_path = save_files.download_img(info[0])
        excel_file.set_row(row_num, 250)
        excel_file.insert_image(row_num, 0, img_path)
        for j in range(1, len(info)):
            excel_file.write(row_num, j, info[j], excel_file_format)
        row_num += 1
    file_object.close()


def spider_johnlewis_sql(sql_instance, product_url):
    spider = Johnlewis(product_url)
    items = {
        "company": "johnlewis",
        "category": "christmas",
    }
    for info in spider.get_all_items():
        items["productName"] = info[1],
        items["productUrl"] = info[3],
        items["productPrice"] = info[2],
        items["productPicUrl"] = info[0],
        items["productPicPath"] = "None",
        items["productMore"] = info[4] if len(info) > 4 else "None"
        sql_instance.db_insert(items)
    sql_instance.db_close()


def mysql_instance():
    con_sql_info = {'host': '49.235.84.115',
                    'port': 6606,
                    'username': 'root',
                    'passwd': 'bxf198592',
                    'db_name': 'jetSpider'}
    return MySQL(**con_sql_info)


if __name__ == "__main__":
    johnlewis = {
        "baubles-tree-decorations": "https://www.johnlewis.com/browse/christmas/baubles-tree-decorations/_/N-54yc",
        "christmas-trees": "https://www.johnlewis.com/browse/christmas/christmas-trees/_/N-54yh",
        "christmas-tree-skirts": "https://www.johnlewis.com/browse/christmas/christmas-tree-skirts/_/N-5miq",
        "christmas-tree-toppers": "https://www.johnlewis.com/browse/christmas/christmas-tree-toppers/_/N-7nrq",
        "christmas-lights": "https://www.johnlewis.com/browse/christmas/christmas-lights/_/N-54yf",
        "christmas-wreaths-garlands": "https://www.johnlewis.com/browse/christmas/christmas-wreaths-garlands/_/N-5mix",
        "christmas-stockings": "https://www.johnlewis.com/browse/christmas/christmas-stockings/_/N-54yk",
        "christmas-room-decorations": "https://www.johnlewis.com/browse/christmas/christmas-room-decorations/_/N-5miw",
        "christmas-party-decorations": "https://www.johnlewis.com/browse/christmas/christmas-party-decorations/_/N-7gmm",
        "advent-calendars": "https://www.johnlewis.com/browse/christmas/advent-calendars/_/N-54yb"
    }

    for name, url in johnlewis.items():
        start_time = time.time()

        # spider_johnlewis(product_url=url, excel_file_name=name)
        spider_johnlewis_sql(mysql_instance(), url)
        print("Spend time: %s" % (time.time() - start_time))
